// const serverUrl = "http://localHost:8280/luna/assist/v1";
//http://183.238.157.204:8280
const serverUrl = "https://www.lncft.net/assistant/luna/assist/v1";

var sm = require('./smHelper.js');
var http = require('./http.js');

var app = getApp();
// 访问全局值
console.log(app.globalData.userInfo);


const header = (functionId = '') => {
    const userInfo = wx.getStorageSync('userInfo');
    let headers = {
        'appId': 'assist',
        'hospitalId': app.globalData.hospitalId,
        'wechatCode': app.globalData.wechatCode,
        'token': userInfo.token,
        'Content-Type': 'application/json',
        'functionId': functionId,
    };

    return headers;
};

// 封装PUT请求
const get = async (url, data) => {
    var newUrl = serverUrl + url;
    console.log(newUrl);
    const newData = sm.encryptReqData(data);
    var res = await http.get(newUrl, newData, header());
    console.log(res);
    var dataSM = sm.decryptSm4(res.data);
    console.log('de res', dataSM);
    return JSON.parse(dataSM);
};

// 封装PUT请求
const getWidthHeder = async (url, data, functionId) => {
    var newUrl = serverUrl + url;
    console.log(newUrl);
    const newData = sm.encryptReqData(data);
    var res = await http.get(newUrl, newData, header(functionId));
    console.log(res);
    var dataSM = sm.decryptSm4(res.data);
    console.log('de res', dataSM);
    return JSON.parse(dataSM);
};

const dataAs = (dicData) => {
    var formDataBuilder = "appid=" + encodeURIComponent(dicData.appid) + '&' +
        "appid=" + encodeURIComponent(dicData.appid) + '&' +
        "data=" + encodeURIComponent(dicData.data) + '&' +
        "sign=" + encodeURIComponent(dicData.sign) + '&' +
        "timestamp=" + encodeURIComponent(dicData.timestamp) + '&' +
        "version=" + encodeURIComponent(dicData.version);
    return formDataBuilder
}
const post = async (url, data) => {
    var newUrl = serverUrl + url;
    console.log(newUrl);
    console.log('data', data);
    const newData = sm.encryptReqData(data);
    console.log('newData', newData);
    var res = await http.post(newUrl, newData, header());
    console.log('post res', res);
    var dataSM = sm.decryptSm4(res.data);
    var resdata = JSON.parse(dataSM);
    console.log('resdata', resdata);
    return resdata;
};

const postWithHeader = async (url, data, functionId) => {
    var newUrl = serverUrl + url;
    console.log(newUrl);
    console.log('data', data);
    const newData = sm.encryptReqData(data);
    console.log('newData', newData);
    var res = await http.post(newUrl, newData, header(functionId));
    console.log('post res', res);
    var dataSM = sm.decryptSm4(res.data);
    var resdata = JSON.parse(dataSM);
    console.log('resdata', resdata);
    return resdata;
};


// 导出http函数
module.exports = {
    get: get,
    post: post,
    getWidthHeder: getWidthHeder,
    postWithHeader: postWithHeader,
};